package mlta.activities;

import java.util.ArrayList;
import java.util.HashMap;

import mlta.buttons.StarButton;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class CommentsActivity extends ListActivity {

	protected Button btn_help;

	protected Button btn_exit;

	protected Button btn_home;

	protected Button btn_refresh;

	protected Button btn_search;

	protected Button btn_new;

	protected Button btn_old;

	protected Button btn_back;

	protected EditText txt;

	protected Button btn_send;

	protected long course_id = -1;

	protected long topic_id = -1;

	protected String username;

	protected boolean novo = false;

	protected String titulo;

	protected String message;

	protected ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.comments);

		course_id = getIntent().getLongExtra("course_id", -1);
		topic_id = getIntent().getLongExtra("topic_id", -1);
		username = getIntent().getStringExtra("username");
		novo = getIntent().getBooleanExtra("novo", false);
		titulo = getIntent().getStringExtra("titulo");
		message = getIntent().getStringExtra("message");

		btn_help = (Button) findViewById(R.id.comments_help);
		btn_exit = (Button) findViewById(R.id.comments_exit);
		btn_home = (Button) findViewById(R.id.comments_home);
		btn_refresh = (Button) findViewById(R.id.comments_refresh);
		btn_search = (Button) findViewById(R.id.comments_search);
		btn_new = (Button) findViewById(R.id.comments_new);
		btn_old = (Button) findViewById(R.id.comments_old);
		btn_back = (Button) findViewById(R.id.comments_back);

		txt = (EditText) findViewById(R.id.comments_edit);
		btn_send = (Button) findViewById(R.id.comments_send);

		btn_help.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(getApplicationContext(),
						"Feature not ready yet!!!", Toast.LENGTH_LONG).show();
			}

		});

		btn_exit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				System.exit(0);
			}

		});

		btn_home.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(CommentsActivity.this,
						CoursesActivity.class);
				intent.putExtra("username", username);
				intent.putExtra("novo", novo);
				CommentsActivity.this.startActivity(intent);
			}

		});

		btn_refresh.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(CommentsActivity.this,
						CommentsActivity.class);
				intent.putExtra("username", username);
				intent.putExtra("course_id", course_id);
				intent.putExtra("topic_id", topic_id);
				intent.putExtra("novo", novo);
				intent.putExtra("titulo", titulo);
				intent.putExtra("message", message);
				CommentsActivity.this.startActivity(intent);
				System.exit(0);
			}

		});

		btn_search.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(CommentsActivity.this,
						SearchActivity.class);
				intent.putExtra("tipo", 3);
				intent.putExtra("username", username);
				intent.putExtra("course_id", course_id);
				intent.putExtra("topic_id", topic_id);
				intent.putExtra("novo", novo);
				intent.putExtra("titulo", titulo);
				intent.putExtra("message", message);
				CommentsActivity.this.startActivity(intent);
			}

		});

		btn_new.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(CommentsActivity.this,
						NewTopicActivity.class);
				intent.putExtra("course_id", course_id);
				intent.putExtra("username", username);
				intent.putExtra("novo", novo);
				intent.putExtra("titulo", titulo);
				intent.putExtra("message", message);
				CommentsActivity.this.startActivity(intent);
				System.exit(0);
			}

		});

		btn_old.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(getApplicationContext(), "No more comments!",
						Toast.LENGTH_LONG).show();
			}

		});

		btn_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(CommentsActivity.this,
						CoursesTopicActivity.class);
				intent.putExtra("id", course_id);
				intent.putExtra("username", username);
				intent.putExtra("novo", novo);
				intent.putExtra("titulo", titulo);
				intent.putExtra("message", message);
				CommentsActivity.this.startActivity(intent);
				System.exit(0);
			}

		});

		ListView listView = (ListView) findViewById(android.R.id.list);

		if (course_id == 0 && topic_id == 0) {

			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "3");
			map.put("name", "MARIA JOSÉ DA SILVA");
			map.put("date", "7d");
			map.put("message", "Alguém sabe o dia da prova?");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "10");
			map.put("name", "ANTONIO MANUEL");
			map.put("date", "3d");
			map.put("message", "Acho que é dia 30/06?");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "5");
			map.put("name", "CARLOS RODRIGUES");
			map.put("date", "1h");
			map.put("message", "O que é que vai cair?");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "1");
			map.put("name", "BIANCA MELO");
			map.put("date", "30m");
			map.put("message", "Acho que até a aula 6.");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("name", "JULIANA RIBEIRO");
			map.put("date", "20s");
			map.put("message", "A aula 7 não cai?");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("name", "BIANCA MELO");
			map.put("date", "7d");
			map.put("message", ":(");
			list.add(map);

			if (getIntent().getBooleanExtra("novo", false)) {
				map = new HashMap<String, String>();
				map.put("count", "0");
				if(!username.equals(""))
					map.put("name", username.toUpperCase());
				else
					map.put("name", "FULANO");
				map.put("date", "1s");
				map.put("message", getIntent().getStringExtra("NovoMsg"));
				list.add(map);
			}

		} else if (course_id == 0 && topic_id == 1) {

			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "3");
			map.put("name", "MARIA JOSÉ DA SILVA");
			map.put("date", "7d");
			map.put("message", "Alguém sabe o dia da prova?");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "10");
			map.put("name", "ANTONIO MANUEL");
			map.put("date", "3d");
			map.put("message", "Acho que é dia 30/06?");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "5");
			map.put("name", "CARLOS RODRIGUES");
			map.put("date", "1h");
			map.put("message", "O que é que vai cair?");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "1");
			map.put("name", "BIANCA MELO");
			map.put("date", "30m");
			map.put("message", "Acho que até a aula 6.");
			list.add(map);

			map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("name", "JULIANA RIBEIRO");
			map.put("date", "20s");
			map.put("message", "A aula 7 não cai?");
			list.add(map);

		} else if (course_id == 0 && topic_id == 5) {

			if (getIntent().getBooleanExtra("novo", false)) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("count", "0");
				if(!username.equals(""))
					map.put("name", username.toUpperCase());
				else
					map.put("name", "FULANO");
				map.put("date", "1s");
				map.put("message", message);
				list.add(map);
			}
			
		} else if (course_id != 0 && topic_id == 1) {
			
			if (getIntent().getBooleanExtra("novo", false)) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("count", "0");
				if(!username.equals(""))
					map.put("name", username.toUpperCase());
				else
					map.put("name", "FULANO");
				map.put("date", "1s");
				map.put("message", message);
				list.add(map);
			}
			
		} else if (course_id == 0 && topic_id > 1) {

			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("name", "BIANCA MELO");
			map.put("date", "7d");
			map.put("message", ":-)");
			list.add(map);

		} 

		SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.comment,
				new String[] { "count", "name", "date", "message" }, new int[] {
						R.id.comment_count, R.id.comment_name,
						R.id.comment_date, R.id.comment_message });

		listView.setAdapter(adapter);

		btn_send.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				HashMap<String, String> map = new HashMap<String, String>();
				map.put("count", "0");
				if(!username.equals(""))
					map.put("name", username.toUpperCase());
				else
					map.put("name", "FULANO");
				map.put("date", "1s");
				map.put("message", txt.getText().toString());
				list.add(map);

				txt.setText("");

				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(txt.getWindowToken(), 0);

				Toast.makeText(getApplicationContext(), "Sent!!!",
						Toast.LENGTH_LONG).show();

			}

		});

	}

	public void starHandler(View v) {

		LinearLayout parent = (LinearLayout) v.getParent();
		StarButton btn_star = (StarButton) parent.getChildAt(0);
		TextView txt_view = (TextView) parent.getChildAt(1);
		if (btn_star.isActived()) {
			btn_star.setActived(false);
			btn_star.setBackgroundResource(R.drawable.ic_star_off);
			txt_view.setText(String.valueOf(Integer.parseInt(txt_view.getText()
					.toString()) - 1));
		} else {
			btn_star.setActived(true);
			btn_star.setBackgroundResource(R.drawable.ic_star_on);
			txt_view.setText(String.valueOf(Integer.parseInt(txt_view.getText()
					.toString()) + 1));
		}

	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(CommentsActivity.this,
				CoursesTopicActivity.class);
		intent.putExtra("id", course_id);
		intent.putExtra("username", username);
		intent.putExtra("novo", novo);
		intent.putExtra("titulo", titulo);
		intent.putExtra("message", message);
		CommentsActivity.this.startActivity(intent);
		System.exit(0);
	}

}